home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 2 of 3.iso / chapter3 / newrect.c < prev    next >
C/C++ Source or Header  |  1996-03-06  |  8KB  |  263 lines

  1.  
  2. #include <windows.h>  
  3. #include <commctrl.h>
  4. #include "newrect.h"  
  5.  
  6.  
  7.  
  8. #if defined (WIN32)
  9.     #define IS_WIN32 TRUE
  10. #else
  11.     #define IS_WIN32 FALSE
  12. #endif
  13.  
  14. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  15. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  16. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  17.  
  18. HINSTANCE hInst;   // current instance
  19.  
  20. LPCTSTR lpszAppName = "MyApp";
  21. LPCTSTR lpszTitle   = "TTM_NEWTOOLRECT"; 
  22.  
  23.  
  24. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  25.  
  26.  
  27. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  28.                       LPTSTR lpCmdLine, int nCmdShow)
  29. {
  30.    MSG      msg;
  31.    HWND     hWnd; 
  32.    WNDCLASS wc;
  33.  
  34.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  35.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  36.    wc.cbClsExtra    = 0;                      
  37.    wc.cbWndExtra    = 0;                      
  38.    wc.hInstance     = hInstance;              
  39.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  40.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  41.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  42.    wc.lpszMenuName  = lpszAppName;              
  43.    wc.lpszClassName = lpszAppName;              
  44.  
  45.    if ( IS_WIN95 )
  46.    {
  47.       if ( !RegisterWin95( &wc ) )
  48.          return( FALSE );
  49.    }
  50.    else if ( !RegisterClass( &wc ) )
  51.       return( FALSE );
  52.  
  53.    hInst = hInstance; 
  54.  
  55.    hWnd = CreateWindow( lpszAppName, 
  56.                         lpszTitle,    
  57.                         WS_OVERLAPPEDWINDOW, 
  58.                         CW_USEDEFAULT, 0, 
  59.                         CW_USEDEFAULT, 0,  
  60.                         NULL,              
  61.                         NULL,              
  62.                         hInstance,         
  63.                         NULL               
  64.                       );
  65.  
  66.    if ( !hWnd ) 
  67.       return( FALSE );
  68.  
  69.    ShowWindow( hWnd, nCmdShow ); 
  70.    UpdateWindow( hWnd );         
  71.  
  72.    while( GetMessage( &msg, NULL, 0, 0) )   
  73.    {
  74.       TranslateMessage( &msg ); 
  75.       DispatchMessage( &msg );  
  76.    }
  77.  
  78.    return( msg.wParam ); 
  79. }
  80.  
  81.  
  82. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  83. {
  84.    WNDCLASSEX wcex;
  85.  
  86.    wcex.style         = lpwc->style;
  87.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  88.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  89.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  90.    wcex.hInstance     = lpwc->hInstance;
  91.    wcex.hIcon         = lpwc->hIcon;
  92.    wcex.hCursor       = lpwc->hCursor;
  93.    wcex.hbrBackground = lpwc->hbrBackground;
  94.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  95.    wcex.lpszClassName = lpwc->lpszClassName;
  96.  
  97.    // Added elements for Windows 95.
  98.    //...............................
  99.    wcex.cbSize = sizeof(WNDCLASSEX);
  100.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  101.                             IMAGE_ICON, 16, 16,
  102.                             LR_DEFAULTCOLOR );
  103.             
  104.    return RegisterClassEx( &wcex );
  105. }
  106.  
  107.  
  108. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  109. {
  110. static TOOLINFO ti;
  111. static int      index;
  112.  
  113. static HWND hToolTip = NULL;
  114.  
  115.  
  116.    switch( uMsg )
  117.    {
  118.       case WM_CREATE :
  119.               {
  120.                  InitCommonControls();
  121.  
  122.                  // Create tool tip control.
  123.                  //.........................
  124.                  hToolTip = CreateWindowEx( 0,
  125.                                             TOOLTIPS_CLASS, NULL, 
  126.                                             TTS_ALWAYSTIP,
  127.                                             CW_USEDEFAULT, 
  128.                                             CW_USEDEFAULT, 
  129.                                             CW_USEDEFAULT, 
  130.                                             CW_USEDEFAULT,
  131.                                             hWnd, NULL, 
  132.                                             hInst, NULL );
  133.  
  134.                  // Add the tools to the tool tip control.
  135.                  //.......................................
  136.                  ti.cbSize = sizeof( TOOLINFO );
  137.                  ti.hwnd   = hWnd;
  138.                  ti.hinst  = NULL;
  139.                  ti.uFlags = TTF_SUBCLASS;
  140.  
  141.                  for( index=0; index < 4; index++ )
  142.                  {
  143.                     ti.uId      = index;
  144.                     ti.lpszText = LPSTR_TEXTCALLBACK;
  145.  
  146.                     ti.rect.top    = 0;
  147.                     ti.rect.bottom = 10;
  148.                     ti.rect.left   = 10*index;
  149.                     ti.rect.right  = 10*(index+1);
  150.  
  151.                     SendMessage( hToolTip, TTM_ADDTOOL, 0, (LPARAM)&ti );
  152.                  }
  153.               }
  154.               break;
  155.  
  156.       case WM_SIZE :
  157.               {
  158.                  // Adjust the tool rectangles for the new
  159.                  // section size.
  160.                  //.......................................
  161.                  RECT     rect;
  162.  
  163.                  ti.cbSize = sizeof( TOOLINFO );
  164.                  ti.hwnd   = hWnd;
  165.  
  166.                  GetClientRect( hWnd, &rect );
  167.  
  168.                  for( index = 0; index < 4; index++ )
  169.                  {
  170.                     ti.uId = index;
  171.  
  172.                     ti.rect.top    = 0;
  173.                     ti.rect.bottom = HIWORD( lParam );
  174.                     ti.rect.left   = (rect.right/4)*index;
  175.                     ti.rect.right  = (rect.right/4)*(index+1);
  176.  
  177.                     SendMessage( hToolTip, TTM_NEWTOOLRECT, 0, (LPARAM)&ti );
  178.                  }
  179.               }
  180.               break;
  181.  
  182.       case WM_PAINT  :
  183.               {
  184.                  // Paint the vertical lines to denote the sections.
  185.                  //.................................................
  186.                  PAINTSTRUCT ps;
  187.  
  188.                  index = 0;
  189.  
  190.                  BeginPaint( hWnd, &ps );
  191.  
  192.                  while ( SendMessage( hToolTip, TTM_ENUMTOOLS, 
  193.                          index, (LPARAM)&ti ) )
  194.                  {
  195.                     MoveToEx( ps.hdc, ti.rect.right, ti.rect.top, NULL );
  196.                     LineTo( ps.hdc, ti.rect.right, ti.rect.bottom );
  197.                     index++;
  198.                  }
  199.  
  200.                  EndPaint( hWnd, &ps );
  201.               }
  202.               break;
  203.  
  204.       case WM_NOTIFY :
  205.               switch ( ((LPNMHDR)lParam)->code )
  206.               {
  207.                  case TTN_NEEDTEXT :
  208.                       wsprintf( ((LPTOOLTIPTEXT)lParam)->szText, 
  209.                                 "Section %d", 
  210.                                 ((LPTOOLTIPTEXT)lParam)->hdr.idFrom+1 );
  211.                       break;
  212.               }
  213.               break;
  214.  
  215.  
  216.       case WM_COMMAND :
  217.               switch( LOWORD( wParam ) )
  218.               {
  219.                  case IDM_ABOUT :
  220.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  221.                         break;
  222.  
  223.                  case IDM_EXIT :
  224.                         DestroyWindow( hWnd );
  225.                         break;
  226.               }
  227.               break;
  228.       
  229.       case WM_DESTROY :
  230.               PostQuitMessage(0);
  231.               break;
  232.  
  233.       default :
  234.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  235.    }
  236.  
  237.    return( 0L );
  238. }
  239.  
  240.  
  241. LRESULT CALLBACK About( HWND hDlg,           
  242.                         UINT message,        
  243.                         WPARAM wParam,       
  244.                         LPARAM lParam)
  245. {
  246.    switch (message) 
  247.    {
  248.        case WM_INITDIALOG: 
  249.                return (TRUE);
  250.  
  251.        case WM_COMMAND:                              
  252.                if (   LOWORD(wParam) == IDOK         
  253.                    || LOWORD(wParam) == IDCANCEL)    
  254.                {
  255.                        EndDialog(hDlg, TRUE);        
  256.                        return (TRUE);
  257.                }
  258.                break;
  259.    }
  260.  
  261.    return (FALSE); 
  262. }
  263.